package com.opensourceedge.phonebook.api;


public class Contact {

	private int contactID;
	private String firstName;
	private String lastName;
	private String mobilePhone;
	private String workPhone;
	private String companyName;
	private String streetName;
	private String buildingNumber;
	private String city;
	private String state;
	private String zipCode;
	
	
	public int getContactID() {
		return contactID;
	}
	public void setContactID(int contactID) {
		this.contactID = contactID;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) throws Exception {
		Contact.validateName(firstName);
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) throws Exception {
		Contact.validateName(lastName);
		this.lastName = lastName;
	}
	public String getMobilePhone() {
		return mobilePhone;
	}
	public void setMobilePhone(String mobilePhone) throws Exception {
		Contact.validatePhone(mobilePhone);
		this.mobilePhone = mobilePhone;
	}
	public String getWorkPhone() {
		return workPhone;
	}
	public void setWorkPhone(String workPhone) throws Exception{
		Contact.validatePhone(workPhone);
		this.workPhone = workPhone;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public String getStreetName() {
		return streetName;
	}
	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}
	public String getBuildingNumber() {
		return buildingNumber;
	}
	public void setBuildingNumber(String buildingNumber) {
		this.buildingNumber = buildingNumber;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	
	/*
	 * Validates contacts class attributes and throws exception.
	 */
	public void validate() throws Exception
	{
		String namePattern="^[A-Za-z]*$";
		String phonePattern="[0-9]{10}";
		//String buildingNumber="\\d";
		
		if(this.firstName.matches(namePattern)!=true){
			throw new Exception("firstNameUnexpectedException");
		}
		if(this.lastName.matches(namePattern)!=true){
			throw new Exception("lastNameUnexpectedException");
		}
		if(this.mobilePhone.matches(phonePattern)!=true){
			throw new Exception("mobilePhoneNumberUnexpectedException");
		}
		if(this.workPhone.matches(phonePattern)!=true){
			throw new Exception("workPhoneUnexpectedException");
		}
	}
	
	/*public static boolean validateEmail(String email){
		String emailPattern="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		return email.matches(emailPattern);
	}*/
	
	public static void validateName(String name) throws Exception{
		String namePattern="^[A-Za-z]*$";
		if(!name.matches(namePattern) && !name.equalsIgnoreCase("null")){
			throw new Exception("namePatternException");
		}
	}
	
	public static void validatePhone(String phone)throws Exception{
		String phonePattern="[0-9]*";
		if(!phone.matches(phonePattern) && !phone.equalsIgnoreCase("null")){
			throw new Exception("phonePatternException");
		};
	}
	
	public static void main(String args[]) throws Exception{
		/*System.out.println("test of validateEmail() method --> "+Contact.validateEmail(".com"));
		System.out.println("Testing for validate first name:");
		Scanner sc=new Scanner(System.in);
		while (sc.hasNext())
		{
			System.out.println(sc.next().matches("^[A-Za-z0-9]*$"));
		}
		sc.close();*/
		Contact con=new Contact();
		con.setContactID(2);
		con.setFirstName("bishwash");
		con.setLastName("adhikari");
		con.setWorkPhone("1412222222");
		con.setMobilePhone("1234123412");
		con.setBuildingNumber("123");
		try {
			con.validate();
		} catch (Exception e) {
			System.out.println("<<--Exception Bishwash adhikari--Just for test-->>");
			e.printStackTrace();
		}
	}
}
